%Author:Xiyin,li
%Date  :2021-7-05
%Copyright 2021 Xiyin,li.

%% Introfuction
%Compare Magnetic induction of the Spherical permanent magnet；
%Use Analytical Model and Dipole Model;

%% STEP1:Set the Parameters
clc,clear
close all
B3d_PM_Ball.Ana = [];
B3d_PM_Ball.Dip = [];
Br = 1.465;%
nImages  = 30;
xd = zeros(1,nImages);
yd = zeros(1,nImages)+linspace(5.1e-3,100e-3,nImages);
zd = zeros(1,nImages);

%% Step2:Calucate the Magnetic induction
for idx = 1:nImages
    B3d_PM_Ball.Ana = [B3d_PM_Ball.Ana PMBall3D(Br,5e-3,[xd(idx);yd(idx);zd(idx)])];
    B3d_PM_Ball.Dip = [B3d_PM_Ball.Dip PMDipBall(5e-3,[xd(idx);yd(idx);zd(idx)])];
end

%% Step3:Plot the difference
D = sqrt(xd.^2+yd.^2+zd.^2);
B1 = sqrt(B3d_PM_Ball.Ana(1,:).^2+B3d_PM_Ball.Ana(2,:).^2+B3d_PM_Ball.Ana(3,:).^2);
B2 = sqrt(B3d_PM_Ball.Dip(1,:).^2+B3d_PM_Ball.Dip(2,:).^2+B3d_PM_Ball.Dip(3,:).^2);
%plotGif(D,B1,B2,'Ball90deg');
hold on
xlabel('Distance(mm)');
ylabel('|B|(T)');
set(gca,'FontSize',20,'Fontname', 'Times New Roman');
axis([1000*min(D),1000*max(D),min([min(B1) min(B2)]),max([max(B1) max(B2)])])
plot1 = plot(1000*D,B1,'b-s','lineWidth',2);
plot2 = plot(1000*D,B2,'r--*','lineWidth',2);
legend('Analytical Model','Dipole Model')
